package com.ny.controller;


import com.ny.entity.Users;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/personalCenter")
public class PersonalCenterController {

    @Autowired
    private UserService userService;

    // 进入个人中心页面，展示用户信息（此处简单示例，可根据实际需求完善）
    @RequestMapping
    public String personalCenter(HttpSession session, Model model ) {
        Users users = (Users) session.getAttribute("user");
        Users updatedUser = userService.getUserById(users.getUserId());
        model.addAttribute("user", updatedUser);
        return "personalCenter";
    }

    // 修改密码页面
    @GetMapping("/changePasswordView")
    public String changePasswordPage() {
        return "changePassword";
    }

    // 处理修改密码请求
    @RequestMapping("/changePassword")
    public String handleChangePassword(@RequestParam("newPassword") String newPassword,
                                       HttpSession session, Model model) {
        Users users = (Users) session.getAttribute("user");
        System.out.println(users.getUserId());
        // 此处可添加验证旧密码是否正确等逻辑，简单示例暂省略
        userService.changePassword(users.getUserId(), newPassword);

        model.addAttribute("message", "密码修改成功！");
        return "personalCenter";
    }

    // 修改手机号码页面
    @GetMapping("/changePhoneNumberView")
    public String changePhoneNumberPage() {
        return "changePhoneNumber";
    }

    // 处理修改手机号码请求
    @PostMapping("/changePhoneNumber")
    public String handleChangePhoneNumber(@RequestParam("newPhoneNumber") String newPhoneNumber,
                                          HttpSession session, Model model) {
        Users users = (Users) session.getAttribute("user");
        userService.changePhoneNumber(users.getUserId(), newPhoneNumber);
        model.addAttribute("message", "手机号码修改成功！");

        return "redirect:/personalCenter";
    }
}